package com.cqupt.scanning.system.statistics.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.admin.entity.*;
import com.cqupt.scanning.system.admin.entity.dto.PatientFootDTO;
import com.cqupt.scanning.system.admin.service.*;
import com.cqupt.scanning.system.statistics.entity.CalculatedDataFoot;
import com.cqupt.scanning.system.statistics.entity.CalculatedDataThree;
import com.cqupt.scanning.system.statistics.mapper.CalculatedDataFootMapper;
import com.cqupt.scanning.system.statistics.service.CalculatedDataThreeService;
import com.cqupt.scanning.system.statistics.service.StatisticsService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Data: 2021/7/8 16:02
 * @Author: 宋宝梁
 */
@Service
public class StatisticsServiceImpl extends ServiceImpl<CalculatedDataFootMapper, CalculatedDataFoot> implements StatisticsService {

    private DeviceFootService deviceFootService;
    private DeviceThreeService deviceThreeService;
    private PatientFootService patientFootService;
    private PatientThreeService patientThreeService;
    private PatientThreeBearingService patientThreeBearingService;
    private CalculatedDataThreeService calculatedDataThreeService;
    private AclUserService aclUserService;


    @Autowired
    public StatisticsServiceImpl(DeviceFootService deviceFootService, PatientFootService patientFootService,
                                 PatientThreeService patientThreeService, DeviceThreeService deviceThreeService,
                                 CalculatedDataThreeService calculatedDataThreeService, AclUserService aclUserService,
                                 PatientThreeBearingService patientThreeBearingService) {
        this.deviceFootService = deviceFootService;
        this.deviceThreeService = deviceThreeService;
        this.patientFootService = patientFootService;
        this.patientThreeService = patientThreeService;
        this.calculatedDataThreeService = calculatedDataThreeService;
        this.aclUserService = aclUserService;
        this.patientThreeBearingService = patientThreeBearingService;


    }

    @Override
    public R queryDevCount() {
        int devFootCount = deviceFootService.count(null);
        int devThreeCount = deviceThreeService.count(null);
        return R.ok().data("devFootCounts", devFootCount).data("devThreeCounts", devThreeCount);
    }

    @Override
    public R queryPatCount() {
        int patFootCount = patientFootService.count(null);
        int patThreeCount = patientThreeService.count(null);
        return R.ok().data("patFootCounts", patFootCount).data("patThreeCounts", patThreeCount);
    }

    @Override
    public R queryAppDoctorCount() {
        int doctorsFootCounts = patientFootService.count(null);
        int doctorsThreeCounts = patientThreeService.count(null);
        return R.ok().data("doctorsFootCounts", doctorsFootCounts).data("doctorsThreeCounts", doctorsThreeCounts);
    }

    @Override
    public R queryReportCount() {
        int patFootCount = patientFootService.count(null);
        int patThreeCount = patientThreeService.count(null);
        return R.ok().data("footReportsCounts", patFootCount).data("threeReportsCounts", patThreeCount);
    }

    @Override
    public R queryDetectDoctorCount() {
        int doctorsFootCounts = patientFootService.count(null);
        int doctorsThreeCounts = patientThreeService.count(null);
        return R.ok().data("doctorsFootCounts", doctorsFootCounts).data("doctorsThreeCounts", doctorsThreeCounts);
    }

    @Override
    public R queryExcelCount(String name, String ageLower, String ageUpper, String doctor, String heightLower, String heightUpper, String measureMode, String sex, String weightLower, String weightUpper, String beginTime, String endTime, String userName, String phone, String orgName) {
        QueryWrapper<CalculatedDataFoot> w = new QueryWrapper<>();
        if (!StrUtil.isEmpty(name)){
            w.like("name",name);
        }
        if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
            w.ge("age", ageLower).le("age", ageUpper);
        }
        if (!StrUtil.isEmpty(doctor)){
            w.like("doctor",doctor);
        }
        if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
            w.ge("height",heightLower).le("height",heightUpper);
        }
        if (!StrUtil.isEmpty(measureMode)){
            w.like("gaitMeasureMode",measureMode);
        }
        if (!StrUtil.isEmpty(sex)){
            w.eq("sex", sex);
        }
        if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
            w.ge("weight",weightLower).le("weight",weightUpper);
        }
        if (!StrUtil.isEmpty(orgName)){
            List<PatientFoot> patientFoots = patientFootService.list(new QueryWrapper<PatientFoot>().like("org_name", orgName));
            List ids = patientFoots.stream().map(PatientFoot::getId).collect(Collectors.toList());
            w.in("patient_foot_id",ids);
        }
        if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
            w.ge("create_time",beginTime).le("create_time",endTime);
        }
        if(!StrUtil.isEmpty(phone)){
            w.eq("phone",phone);
        }
        if (!StrUtil.isEmpty(userName)){
            AclUser user = aclUserService.getOne(new QueryWrapper<AclUser>().eq("note",0).eq("username", userName).select("id"));
            if(user==null){
                return R.error().message("未查询到"+userName+"的相关数据");
            }
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().eq("user_id", user.getId()).select("dev_num"));
            List<PatientFoot> patientFoots = patientFootService.list(new QueryWrapper<PatientFoot>().eq("dev_num", deviceFoot.getDevNum()));
            List ids = patientFoots.stream().map(PatientFoot::getId).collect(Collectors.toList());
            w.in("patient_foot_id",ids);
        }

        List<CalculatedDataFoot> list = baseMapper.selectList(w);
        return R.ok().data("excelFootCounts", list);
    }

    @Override
    public R queryThreeExcelCount (String name,String ageLower,String ageUpper,String doctor,String heightLower,String heightUpper,String measureMode,String sex,String weightLower,String weightUpper,String beginTime,String endTime,String userName,String phone,String businessName,String address,String cityProvince,String country,String fax,
                                   String weightBearingLower, String weightBearingUpper,String dressShoe,String athleticShoe,String casualShoe,String heels,String age){
        QueryWrapper<CalculatedDataThree> w = new QueryWrapper<>();
        if (!StrUtil.isEmpty(name)){
            w.like("name",name);
        }
        if (!StrUtil.isEmpty(age)){
            w.like("age",age);
        }
        if (!StrUtil.isEmpty(ageLower) && !StrUtil.isEmpty(ageUpper)){
            w.ge("age", ageLower).le("age", ageUpper);
        }
        if (!StrUtil.isEmpty(doctor)){
            w.like("doctor",doctor);
        }
        if (!StrUtil.isEmpty(heightLower) && !StrUtil.isEmpty(heightUpper)){
            w.ge("height",heightLower).le("height",heightUpper);
        }
        if (!StrUtil.isEmpty(measureMode)){
            w.like("gaitMeasureMode",measureMode);
        }
        if (!StrUtil.isEmpty(sex)){
            w.eq("sex", sex);
        }
        if (!StrUtil.isEmpty(weightLower) && !StrUtil.isEmpty(weightUpper)){
            w.ge("weight",weightLower).le("weight",weightUpper);
        }
        if (!StrUtil.isEmpty(beginTime) && !StrUtil.isEmpty(endTime)){
            w.ge("create_time",beginTime).le("create_time",endTime);
        }
        if(!StrUtil.isEmpty(phone)){
            w.eq("phone",phone);
        }
        if(!StrUtil.isEmpty(businessName)){
            w.like("business_name",businessName);
        }
        if(!StrUtil.isEmpty(address)){
            w.like("address",address);
        }
        if(!StrUtil.isEmpty(cityProvince)){
            w.like("city_province",cityProvince);
        }
        if(!StrUtil.isEmpty(country)){
            w.like("country",country);
        }
        if(!StrUtil.isEmpty(fax)){
            w.like("fax",fax);
        }
        if (!StrUtil.isEmpty(weightBearingLower) && !StrUtil.isEmpty(weightBearingUpper)){
            w.ge("weight_bearing",weightBearingLower).le("weight_bearing",weightBearingUpper);
        }
        if(!StrUtil.isEmpty(dressShoe)){
            w.like("dress_shoe",dressShoe);
        }
        if(!StrUtil.isEmpty(athleticShoe)){
            w.like("athletic_shoe",athleticShoe);
        }
        if(!StrUtil.isEmpty(casualShoe)){
            w.like("casual_shoe",casualShoe);
        }
        if(!StrUtil.isEmpty(heels)){
            w.like("heels",heels);
        }
        if (!StrUtil.isEmpty(userName)){
            AclUser user = aclUserService.getOne(new QueryWrapper<AclUser>().eq("note",0).eq("username", userName).select("id"));
            if(user==null){
                return R.error().message("未查询到"+userName+"的相关数据");
            }
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().eq("user_id", user.getId()).select("dev_num"));
            List<PatientThree> patientThrees = patientThreeService.list(new QueryWrapper<PatientThree>().eq("dev_num", deviceThree.getDevNum()));
            List<PatientThreeBearing> patientThreeBearings = patientThreeBearingService.list(new QueryWrapper<PatientThreeBearing>().eq("dev_num", deviceThree.getDevNum()));
            List ids1 = patientThrees.stream().map(PatientThree::getId).collect(Collectors.toList());
            List ids2 = patientThreeBearings.stream().map(PatientThreeBearing::getId).collect(Collectors.toList());
            w.in("patient_three_id",ids1).or().in("patient_three_id",ids2);
        }

        List<CalculatedDataThree> threeExcel = calculatedDataThreeService.list(w);
        ArrayList<Object> threeExcelList = new ArrayList<>();
        threeExcelList.addAll(threeExcel);
        return R.ok().data("excelThreeCounts", threeExcelList);
    }
}
